<?php
namespace app\company\controller;

use app\common\controller\Comm;
use app\common\model\SuppliersCompany;
use think\Cache;
use think\Controller;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

/**
 * 实时数据控制器
 * Class Realdata
 * @package app\gygt\controller
 */
class User extends Comm
{
    public function __construct() {
        header("Access-Control-Allow-Origin:*");
        header("Access-Control-Allow-Method:POST,GET,OPTIONS");
        header("Access-Control-Allow-Headers:Authorization,x-csrf-token,X-Requested-With, Cache-Control, Expires, Content-Type");
    }

    /***
     * 用户登录
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login(){
        $uname = input('username','','trim');
        $passwd = input('password','','trim');

        //1 参数验证是否合法
        if (empty($uname)){
            json_error_msg("用户名不能为空！请检查");
        }elseif (empty($passwd)){
            json_error_msg("密码不能为空! 请检查");
        }

        $model = new SuppliersCompany();
        //2数据处理
        $passwd = md5($passwd);
        $con['username'] = $uname;
        $con['password'] = $passwd;

        $res =  $model->where($con)->find();
        if (empty($res)){
            json_return_code('',"用户名或密码错误！",201);
        }
        json_return_code($res);
    }

    public function modifyCompany()
    {
        $id = input('id');
        $data = $this->trimParam();

        $data['service_brand'] = $data['service_brand']?str_replace('，',',',$data['service_brand']):'';
        $model = new SuppliersCompany();
        $data['id'] = $id;
        $data['update_time'] = date('Y-m-d H:i:s');
        $result = $this->validate($data,'SuppliersCompany.save');
        if ($result !== true) json_return_code('',$result,201);
        $res = $model->allowField(true)->isUpdate(true)->save($data,array('id'=>$id));
        $msg = '修改';
        addLog('修改供应商信息');
        if($res !== false){
            $info = $model->where('id',$id)->find();
            json_return_code($info,$msg.'成功');
        }else{
            json_return_code('',$msg.'失败', '201');
        }
    }
    /***
     * 修改密码
     */
    public function update_password(){
        $org_pwd = input('oldPsw');
        $new_pwd = input('newPsw');
        $id = input('id');
        if (empty($id)){
            json_error_msg("用户id不能为空，请检查！");
        }
        if(empty($org_pwd)){
            json_error_msg("原密码不能为空，请检查！");
        }
        if(empty($new_pwd)){
            json_error_msg("新密码不能为空，请检查！");
        }

        $model = new SuppliersCompany();
        $user = $model->where(array('id'=>$id))->find();

        if($user['password'] != md5($org_pwd) ){
            json_error_msg("原密码不正确，请检查！");
        }

        $res = $model->where(array('id'=>$id))->update(array('password'=>md5($new_pwd)));
        json_success_msg($res);
    }

    public function getMd5Str()
    {
        $words = input('words','','trim');
        $str = md5($words);
        json_return_code($str);
    }
}
